import { ApigetUsePatientList , ApicheckPatient, ApidelPatient } from "../../../api/index"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    useList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getList()
    this.onRefresh = app.event.on("visitorListRefresh",this.getList)
  },

  onUnload() {
    this.onRefresh.removeAllListeners("visitorListRefresh")
  },

  getList() {
    return new Promise(resolve => {
      ApigetUsePatientList().then(res => {
        this.setData({
          useList: res.data
        })
      }).finally(_=>{
        resolve()
      })
    })
    
  },

  onChangeCheck(e) {
    const item = e.currentTarget.dataset.item
    wx.showModal({
      title: '提示',
      content: '确定要切换就诊人吗？',
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({
            title: '切换中',
          })
          ApicheckPatient(item.id)
          .then(_=>this.getList())
          .then(_=>{
            app.setUserInfo(item)
            app.setWebScoket()
            wx.showToast({
              title: '切换成功',
              icon: "success"
            })
          })
          .catch(_=>{
            wx.hideLoading()
          })
        }
      }
    })
  },

  goVisitorEdit() {
    wx.navigateTo({
      url: '/pages/user/visitorEdit/index',
    })
  },

  onEdit(e){
    const item = e.currentTarget.dataset.item
    const ops = {
      id: item.id,
      name: item.name,
      gender: item.gender,
      birth: item.birth,
      id_number: item.id_number,
      nation: item.nation,
      area: item.area || [],
      area_detail: item.area_detail,
      phone: item.phone,
    }
    wx.navigateTo({
      url: '/pages/user/visitorEdit/index?item=' + JSON.stringify(ops),
    })
  },

  onDel(e) {
    wx.showModal({
      title: '提示',
      content: '确定要删除这条就诊信息吗？',
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({
            title: '删除中',
          })
          ApidelPatient(e.currentTarget.dataset.id)
          .then(_=>this.getList())
          .then(_=>{
            wx.showToast({
              title: '删除成功',
              icon: "success"
            })
          })
          .catch(_=>{
            wx.hideLoading()
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})